#!/bin/bash
. $PROJECT_BASE/src/test/resources/testEnv.sh
. $PROJECT_BASE/src/main/scripts/utils/utils.sh

testCreateSimpleField(){
    local ESPECTED="\"title\": \"SCM Metrics \\\"QA Card\\\"\""
    local result=( $(createField "title" "SCM Metrics \"QA Card\"") )
    local CALCULATED=`echo ${result[@]}`
    assertEquals "$ESPECTED" "$CALCULATED"
}

testCreateRowElement(){
    local ESPECTED="[\"10/07/2010\",\"1236\",\"45\",\"5\",\"3\",\"444\",\"10\"]"
    local result=( $(createRowElement "10/07/2010" "1236" "45" "5" "3" "444" "10") )
    local CALCULATED=`echo ${result[@]}`
    assertEquals "$ESPECTED" "$CALCULATED"
}

testCreateArrayField(){
    local ESPECTED="\"dades\": [[\"10/07/2010\",\"1236\",\"45\",\"5\",\"3\",\"444\",\"10\"],[\"10/08/2010\",\"2236\",\"35\",\"2\",\"14\",\"244\",\"20\"]]"
    local result=( $(createRowElement "10/07/2010" "1236" "45" "5" "3" "444" "10") )
    local row0=`echo ${result[@]}`
    local result=( $(createRowElement "10/08/2010" "2236" "35" "2" "14" "244" "20") )
    local row1=`echo ${result[@]}`
    local result=( $(createArrayField "dades" "${row0} ${row1}" ))
    local CALCULATED=`echo ${result[@]}`
    assertEquals "$ESPECTED" "$CALCULATED"
}

testAddFieldToJsonObject(){
    local ESPECTED="{\"title\": \"SCM Metrics\", \"description\": \"Métricas de utilización del repositorio de fuentes\", \"dades\": [[\"10/07/2010\",\"1236\",\"45\",\"5\",\"3\",\"444\",\"10\"],[\"10/08/2010\",\"2236\",\"35\",\"2\",\"14\",\"244\",\"20\"]]}"
    local result=( $(createField "title" "SCM Metrics") )
    local field=`echo ${result[@]}`
    local result=( $(addFieldToJsonObject  "" "${field}") )
    jsonObject=`echo ${result[@]}`
    result=( $(createField "description" "Métricas de utilización del repositorio de fuentes") )
    field=`echo ${result[@]}`
    local result=( $(addFieldToJsonObject  "${jsonObject}" "${field}") )
    jsonObject=`echo ${result[@]}`
    local result=( $(createRowElement "10/07/2010" "1236" "45" "5" "3" "444" "10") )
    local row0=`echo ${result[@]}`
    local result=( $(createRowElement "10/08/2010" "2236" "35" "2" "14" "244" "20") )
    local row1=`echo ${result[@]}`
    local result=( $(createArrayField "dades" "${row0} ${row1}" ))
    field=`echo ${result[@]}`
    local result=( $(addFieldToJsonObject  "${jsonObject}" "${field}") )
    local CALCULATED=`echo ${result[@]}`
    assertEquals "$ESPECTED" "$CALCULATED"
}
$SHUNIT2